{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c497ad3d-6c46-44f3-b0da-aba99f0a45e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch  \n",
    "from torch import nn  \n",
    "from torch.nn import functional as F  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "48932c17-9985-4fd4-984c-f1ca4294fa77",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MultipleKernelLayer(nn.Module):  \n",
    "    def __init__(self, kernels, dim_out):  \n",
    "        super(MultipleKernelLayer, self).__init__()  \n",
    "        self.kernels = nn.ModuleList(kernels)  \n",
    "        self.dim_out = dim_out  \n",
    "        self.kernel_weights = nn.Parameter(torch.randn(len(kernels)))  \n",
    "  \n",
    "    def forward(self, x):  \n",
    "        # 假设x是一个列表，其中每个元素对应一个核的输入  \n",
    "        outputs = [kernel(xi) for kernel, xi in zip(self.kernels, x)]  \n",
    "        weighted_outputs = [self.kernel_weights[i] * output for i, output in enumerate(outputs)]  \n",
    "        combined_output = sum(weighted_outputs)  \n",
    "        return combined_output  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "29cedf29-0aa1-49a2-a231-bc34436872b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 示例：定义两个简单的核函数  \n",
    "class LinearKernel(nn.Module):  \n",
    "    def __init__(self, dim_in, dim_out):  \n",
    "        super(LinearKernel, self).__init__()  \n",
    "        self.linear = nn.Linear(dim_in, dim_out)  \n",
    "  \n",
    "    def forward(self, x):  \n",
    "        return self.linear(x)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "be53420a-4d33-446d-91b5-691d0abc5e3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "class PolyKernel(nn.Module):  \n",
    "    def __init__(self, dim_in, dim_out, degree=3):  \n",
    "        super(PolyKernel, self).__init__()  \n",
    "        self.linear = nn.Linear(dim_in, dim_out)  \n",
    "        self.degree = degree  \n",
    "  \n",
    "    def forward(self, x):  \n",
    "        poly_features = torch.pow(x, self.degree)  \n",
    "        return self.linear(poly_features)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c0a1b74e-f876-4be0-91fb-942b87a21c91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.8791]], grad_fn=<AddBackward0>)\n"
     ]
    }
   ],
   "source": [
    "# 创建多核层  \n",
    "dim_in = 10  \n",
    "# 最终的输出就是分类结果\n",
    "dim_out = 1 \n",
    "\n",
    "\n",
    "kernels = [LinearKernel(dim_in, dim_out), PolyKernel(dim_in, dim_out)]  \n",
    "mkl_layer = MultipleKernelLayer(kernels, dim_out)  \n",
    "  \n",
    "# 示例输入  \n",
    "x1 = torch.randn(1, dim_in)  # 对应LinearKernel的输入  \n",
    "x2 = torch.randn(1, dim_in)  # 对应PolyKernel的输入  \n",
    "x = [x1, x2]  \n",
    "  \n",
    "# 前向传播  \n",
    "output = mkl_layer(x)  \n",
    "print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d11b855-87df-4017-9bae-90bb56651a2d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
